Functions¶
Writing code is like painting.
There are techniques to learn, like color-mixing, brush strokes, layering, shading, perspective, etc.
There is also creative intuition: the ability to exercise the techniques you know to create something beautify.
To build beautiful code, you'll need to know the techniques, and you'll need to develop creative intuition.
Over the next few days, we'll talk about important techniques and practice, practice, practice!
def
¶
You can define more than one function in a script.
three_reds.py
¶
snapshot
¶
Bit has the ability to take a snapshot of the world using the snapshot
function.
snap_three_reds.py
¶
You can use any phrase you want when making a snapshot.
The Jump
buttons take you to the previous or next snapshot.
Naming variables¶
cosmo.py
¶
When naming a function or variable:
- Use underscore
_
(next to the zero key + shift) to break up compound namesgo_green
instead ofgogreen
orgoGreen
- use lower-case characters
go
instead ofGo
orGO
- Casing matters:
go
is different fromGo
!
In blue_squares.py
, when will Bit paint a blue square?
blue_squares.py
¶
Defining a function is not the same as calling a function.
Docstrings¶
The very first line after def
can have a string. When this is the case, we call that string a docstring because it documents the function: you use it to describe what the function is for and how to use it.
Strings that use single quotation characters (e.g. "
or '
) are only one line.
If you want to use multiple lines for your string, use triple quotes ("""
or '''
).
Smiles 😁 😃 🙂 🙃¶
Given the following starting world:
Draw four blue smiles:
smiles.py
¶
- Draw out a strategy
- Identify the pieces
- What are the ideas, how do they fit together?
- Implement one piece at a time
Key Ideas¶
- Define functions with
def
- Make functions to represent complex ideas: abstraction
- Make functions to define tasks that will be repeated
- Naming functions and variables
- Docstrings